package com.chendongyang.www.dao;
import  com.chendongyang.www.po.appointment;
import com.mysql.cj.jdbc.Driver;
import  com.chendongyang.www.po.appointment1;
import java.sql.*;
import java.util.ArrayList;

public class reservations {
    public ArrayList<appointment> select() throws SQLException {
        //查询可用于充电的充电口
        ArrayList<appointment> List=new ArrayList<>();
        DriverManager.registerDriver(new Driver());
        Connection root=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle","root","123456");
        String sql="select * from  充电预约 where 预约用户=0;";
        //PreparedStatement preparedStatement = root.prepareStatement(sql);
        Statement statement=root.createStatement();
        ResultSet resultSet=statement.executeQuery(sql);
        //System.out.println(resultSet.getString(1));
        while(resultSet.next()){
            appointment a=new appointment();
            a.setChargeS(resultSet.getString(1));
            a.setChargeB(resultSet.getString(2));
            a.setTime(resultSet.getString(3));
            List.add(a);
        }
        root.close();
        statement.close();
        resultSet.close();
        return List;
    }
    public  int update(String station,String bit,String id) throws SQLException {
        //客户对充电口进行选择充电；
        DriverManager.registerDriver(new Driver());
        Connection root = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle", "root", "123456");
        String sql="update 充电预约 set 预约用户=? where 充电口 =? and 充电位=?;";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        preparedStatement.setString(1,id);
        preparedStatement.setString(2,station);
        preparedStatement.setString(3,bit);
        int line =0;
        line = preparedStatement.executeUpdate();
        root.close();
        preparedStatement.close();
        return line;
    }
    public ArrayList<appointment1> select1() throws SQLException {
        //查询可用于充电的充电口
        ArrayList<appointment1> List=new ArrayList<>();
        DriverManager.registerDriver(new Driver());
        Connection root=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle","root","123456");
        String sql="select * from  停车预约 where 预约用户=0;";
        //PreparedStatement preparedStatement = root.prepareStatement(sql);
        Statement statement=root.createStatement();
        ResultSet resultSet=statement.executeQuery(sql);
        //System.out.println(resultSet.getString(1));
        while(resultSet.next()){
            appointment1 a=new appointment1();
            a.setParkingS(resultSet.getString(1));
            a.setParkingB(resultSet.getString(2));
            a.setTime(resultSet.getString(3));
            List.add(a);
        }
        root.close();
        statement.close();
        resultSet.close();
        return List;
    }
    public  int update1(String station,String bit,String id) throws SQLException {
        //客户对充电口进行选择充电；
        DriverManager.registerDriver(new Driver());
        Connection root = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle", "root", "123456");
        String sql="update 停车预约 set 预约用户=? where 停车口 =? and 停车位=?;";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        preparedStatement.setString(1,id);
        preparedStatement.setString(2,station);
        preparedStatement.setString(3,bit);
        int line =0;
        line = preparedStatement.executeUpdate();
        root.close();
        preparedStatement.close();
        return line;
    }
}
